//
//  WelcomeViewController.swift
//  HMWeibo08
//
//  Created by 唐万超 on 15/8/1.
//  Copyright © 2015年 唐万超. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    /// 定义图像底部约束
    private var bottomContr: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUI()
        
        // 设置用户头像
        //iconView.sd_setImageWithURL(NSURL(string: (UserAccount.loadAccount()?.avatar_large)!))
        if let urlStr = UserAccount.loadAccount()?.avatar_large {
        
            iconView.sd_setImageWithURL(NSURL(string: urlStr)!)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        bottomContr?.constant = UIScreen.mainScreen().bounds.height - bottomContr!.constant - 50
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
             self.view.layoutIfNeeded()
            }, completion: { (_) -> Void in
                
                // 动画显示完毕，发送通知，跳转页面
                NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewSwitchNotification, object: true)
        })
        
    }

    
    /// 设置UI
    private func setUI() {
    
        view.addSubview(backImage)
        view.addSubview(iconView)
        view.addSubview(textLabel)
        
        
        // 设置约束
        backImage.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImage]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        bottomContr = view.constraints.last
        
        
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
   
    
    
    // MARK: - 懒加载控件
    // 欢迎页面的背景图
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像信息
    private lazy var iconView:UIImageView = {
    
        let iconImg = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconImg.layer.masksToBounds = true
        iconImg.layer.cornerRadius = 45
        
        return iconImg
        
    }()
   
    /// 文字信息
    private lazy var textLabel: UILabel = {
    
        let textLbl = UILabel()
        
        textLbl.text = "欢迎归来"
        textLbl.textColor = UIColor.darkGrayColor()
        
        textLbl.sizeToFit()
        return textLbl
        
    }()

}
